【发布时间】:2017-09-18 02:21:02
【问题描述】:
我有以下表格和输入框。但是,当输入框中没有任何内容时,javascript 不会执行。我需要检测是否有人在不填写表格的情况下点击了“提交”。我已经尝试了很多我在 SO 上找到的东西,但都没有奏效。当有输入值时,一切都可以完美执行js。但是,当没有任何内容时,console.log(bidz) 不会产生任何内容,并且看起来好像脚本退出了。从我读过的内容来看,我知道输入框没有值就不存在。但后来我试图说它是否不存在,然后说什么,但这也没有用。
也许这与我给它一个占位符值“输入一些东西”有关。
<form action="" method="post">
<div id="<?php echo $this->item['id']; ?>">
<div class="ab">
<label for="ab_input"><?php echo $this->translate('To get the auction started, you must enter a starting bid.'); ?></label>
<span class="auction_bid_container">
<input id="ab_input" type="text" maxlength="12"
class="middle nmr event-clear-focus"
name="bidz" value="Enter something" />
<input id="updateBidButton" type="submit"
class="button grey-button num-items-button"
name="bidz"
value="<?php echo $this->translate('submit'); ?>"/>
</span>
</div>
</div>
</form>
<div class="clear mb20"></div>
这是我的 js 函数。我已经尝试了各种方法,但如果没有值,“this.value”会导致错误:
$('input[name="bid_amount"]').live('change', function () {
var bidz = this.value;
console.log(bidz);
$.ajax({
type: 'post',
url: "?module=is&controller=index&action=syy",
dataType: "text",
data: 'bid=' + bid_amount + '&id=' + id,
beforeSend: function () {
$('.ab').animate({
'backgroundColor': '#ffdead'
}, 400);
},
success: function (result) {
if (result == 'ok') {
console.log(bid_amount);
$('.ab').animate({
'backgroundColor': '#A3D1A3'
}, 300);
} else {
$('.ab').animate({
'backgroundColor': '#FFBFBF'
}, 300);
}
}
});
});
【问题讨论】:
标签: javascript jquery html inputbox