我正在嘗試JavaScript,但我是一個紅寶石的傢伙,真的很吸食javascript。我有一個字段形式(文本字段和提交按鈕),其中JavaScript只允許在字段中有文本時提交表單。但是,我將字段類型從文本字段切換到了select。現在,表單無法提交。我幾乎可以肯定的問題是我的JavaScript。這是我用文本字段包含該詞的代碼。我如何使它與選擇一起工作?當從文本字段切換到選擇時javascript停止工作
形式:
<form id="new_skill" class="new_skill" method="post" action="/skills" >
<li>
<input id="resume-field" class="field field288" type="text"
value="Type a speciality you want to add to your profile"
title="Type a speciality you want to add to your profile"
name="skill[label]"></input>
</li>
<li>
<input class="button button-add button-add-disabled"
type="submit" value="ADD +" name="commit"></input>
</li>
</form>
的Javascript:
(function($){
$(document).on('focusin', '#resume-field', function() {
$(this).parents().find('.button-add-disabled').removeClass('button-add-disabled');
}).on('focusout', '#resume-field', function(){
if(this.value==' '||this.title==this.value) {
$(this).parents().find('.button-add').addClass('button-add-disabled');
} else {
$(this).parents().find('.button-add').removeClass('button-add-disabled');
}
});
$('.button-add-disabled').click(function(){
return !$(this).hasClass('button-add-disabled');
});
}(jQuery));
CSS:
.button-add { width: 49px; height: 28px; border: solid 1px #8c8c8c; display: block;
font-size: 11px; line-height: 28px ; color: #fff; text-align: center;
font-family: 'Ubuntu', sans-serif; transition: none; margin: 0 0 0 auto;
border-radius: 3px; }
.button-add:hover { text-decoration: none;
-webkit-transition:none;
-moz-transition:none;
-ms-transition:none;
-o-transition:none;
transition:none;
}
.button-add-disabled { background: url(/assets/add-specialities-disabled.png)
repeat-x 0 0; box-shadow: 0 0 0 0; margin-left:35px; }
.button-add-disabled:hover { background: url(/assets/add-specialities-disabled.png)
repeat-x 0 0; box-shadow: 0 0 0 0; }
HTML中的下拉形式:
<select id="resume-field" name="skill[label]">
<option value="1" title="option_1"></option>
<option value="2" title="option 2" selected="selected"></option>
</select>
可能重複(HTTP:// stackove rflow.com/questions/21053295/jquery-click-function-not-being-called) –
它不是重複的。當我用它來處理文本字段時,我領先於他。 – Philip7899
您可以發佈SELECT的HTML嗎?如果將文本字段轉換爲SELECT,可能有很多原因導致它無法正常工作(例如,不是OPTION元素,ID不匹配等) –