使用 jquery 获取空输入字段中占位符的值?
Get value of placeholder in empty input field with jquery?
有点奇怪,我很困惑。
当我有一个带占位符的输入字段时,例如:
<input id="box1" placeholder="not applicable">
我一直能够获取用户输入,或者如果输入为 空,占位符值 jquery 如下:
var text = $('#box1').val();
没什么大不了的。使用 jquery .val()
总能奏效。然后我开始在以前工作的应用程序中看到一些代码输出错误,其中空输入没有捕获占位符值。
javascript 引擎有什么变化可以解决这个问题吗?或者如果输入字段为空,是否有一种获取占位符值的新方法?
看这个例子:
$('#done').on('click', function(){
if($('#box3').val() == ''){
var box3 = $(this).data('placeholder');
} else {
var box3 = $('#box3').val();
}
$('#output').val(box3);
});
// Clicking the button doesn't capture the placeholder value if empty?
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<input id="box3" placeholder="not applicable">
<br><br>
<button id="done">
Done
</button>
<br><br>
<textarea id="output"></textarea>
<br><br>
<p>
Why doesn't the button click capture the value of the placeholder in this instance, using jquery 3.1.1?
</p>
使用attr('placeholder')
。此外,您在 if
语句中使用 this
是错误的。它引用了 click
函数的主题。
$('#done').on('click', function(){
if($('#box3').val() == ''){
var box3 = $('#box3').attr('placeholder');
} else {
var box3 = $('#box3').val();
}
$('#output').val(box3);
});
// Clicking the button doesn't capture the placeholder value if empty?
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<input id="box3" placeholder="not applicable">
<br><br>
<button id="done">
Done
</button>
<br><br>
<textarea id="output"></textarea>
<br><br>
<p>
Why doesn't the button click capture the value of the placeholder in this instance, using jquery 3.1.1?
</p>
有点奇怪,我很困惑。
当我有一个带占位符的输入字段时,例如:
<input id="box1" placeholder="not applicable">
我一直能够获取用户输入,或者如果输入为 空,占位符值 jquery 如下:
var text = $('#box1').val();
没什么大不了的。使用 jquery .val()
总能奏效。然后我开始在以前工作的应用程序中看到一些代码输出错误,其中空输入没有捕获占位符值。
javascript 引擎有什么变化可以解决这个问题吗?或者如果输入字段为空,是否有一种获取占位符值的新方法?
看这个例子:
$('#done').on('click', function(){
if($('#box3').val() == ''){
var box3 = $(this).data('placeholder');
} else {
var box3 = $('#box3').val();
}
$('#output').val(box3);
});
// Clicking the button doesn't capture the placeholder value if empty?
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<input id="box3" placeholder="not applicable">
<br><br>
<button id="done">
Done
</button>
<br><br>
<textarea id="output"></textarea>
<br><br>
<p>
Why doesn't the button click capture the value of the placeholder in this instance, using jquery 3.1.1?
</p>
使用attr('placeholder')
。此外,您在 if
语句中使用 this
是错误的。它引用了 click
函数的主题。
$('#done').on('click', function(){
if($('#box3').val() == ''){
var box3 = $('#box3').attr('placeholder');
} else {
var box3 = $('#box3').val();
}
$('#output').val(box3);
});
// Clicking the button doesn't capture the placeholder value if empty?
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<input id="box3" placeholder="not applicable">
<br><br>
<button id="done">
Done
</button>
<br><br>
<textarea id="output"></textarea>
<br><br>
<p>
Why doesn't the button click capture the value of the placeholder in this instance, using jquery 3.1.1?
</p>