[英]javascript value to input box
I have a plugin that I am trying to grab the the number of the current slide which will then be written to a input box with the ID of "input1" each time the slide is changed. 我有一个插件,我试图获取当前幻灯片的编号,然后在每次更换幻灯片时将其写入ID为“ input1”的输入框中。 Does my syntax make sense?
我的语法有意义吗?
function currentSlide(){
var current = $('#slider').data('AnythingSlider').currentPage;
document.getElementById("input1").innerHTML=current;
};
If input1
is an input (as the name implies) use: 如果
input1
是输入(顾名思义),请使用:
document.getElementById("input1").value = current;
Or to use jQuery only: 或仅使用jQuery:
function currentSlide(){
var current = $('#slider').data('AnythingSlider').currentPage;
$("#input1").val(current);
};
If you're trying to change what is shown in an <input>
elemnt, set the .value
, NOT the .innerHTML
. 如果要更改
<input>
.innerHTML
,请设置.value
而不是.innerHTML
。
Think about it. 想一想。 Do you write
<input>Blah blah blah</input>
, or do you write <input value="Blah blah blah" />
? 您是写
<input>Blah blah blah</input>
还是写<input value="Blah blah blah" />
? JavaScript treats elements the same way HTML does. JavaScript对元素的处理方式与HTML相同。
function currentSlide(){
var current = $('#slider').data('AnythingSlider').currentPage;
$('input1').val(current);
};
If you're using jQuery, I recommend sticking with jQuery selectors throughout your code. 如果您使用的是jQuery,建议您在整个代码中坚持使用jQuery选择器。 I've updated line 3 to use jQuery to select
input1
. 我已经更新了第3行,以使用jQuery选择
input1
。 Instead of using innerHTML
, use jQuery's val
function which will set input1
's value to the current
string. 代替使用
innerHTML
,使用jQuery的val
函数,该函数会将input1
的值设置为current
字符串。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.