我有一个表单,其中包含 n 个多项选择题,实现为单选按钮(使用 jquery-ui
)。在允许用户提交表单之前,我想确保所有问题都已得到回答。我在执行此验证时遇到了两个问题:
- 在初始加载时,提交按钮不会响应点击或鼠标悬停,直到所有问题都根据需要选择了响应。但是,它似乎仍然可以点击(我希望它可以淡出)
- 如果按下重置按钮,提交按钮会淡出(根据需要),但我无法在表单完成后重新启用提交按钮。
这是我第一次使用 javascipt/jquery。出了什么问题?如何在这两种情况下实现所需的功能?
这是相关的 javascript 部分:
$(document).ready(function(){
$( '.button' ).button ();
$( '.radio-set' ).buttonset ();
$( 'input[type="submit"]' ).attr( 'disabled', true );
var progress = 0;
var total_fields = $('.response').length
/* Track Progress function*/
$('input[type="radio"]').click( function(){
progress = $('input[type="radio"]:checked').length
$(".progressbar").progressbar( {value: ( progress / total_fields ) * 100} )
console.log(progress, total_fields)
if( progress == total_fields ){
console.log( "Hello from inside the conditional" )
$( 'input[type="submit"]' ).removeAttr( 'disabled' );
}
});
$( 'input[type="reset"]' ).click( function(){
progress = 0
$( ".progressbar" ).progressbar( {value: 0} )
$( 'input[type="submit"]' ).attr( 'disabled', true );
});
});
表单中其中一个问题的示例(带有培根填充文本):
<div class="model_feature">
<span class="feature_name" title="M1">M1</span>
<span class="response radio-set">
<label for="M1-1">1</label>
<input type="radio" name="M1" id="M1-1" value="1" class="feature-input" autocomplete="off" />
<label for="M1-0">0</label>
<input type="radio" name="M1" id="M1-0" value="0" class="feature-input" autocomplete="off" />
<label for="M1-Unk">Unknown</label>
<input type="radio" name="M1" id="M1-Unk" value="Unknown" class="feature-input" autocomplete="off" />
</span <!-- close response -->
<span class="feature_description">Chicken spare ribs capicola beef brisket hamburger. Kielbasa filet mignon tail ribeye ball tip ground round.</span>
</div> <!-- close model_feature -->
完整性的输入和重置按钮:
<input type="reset" class="button" id="reset-button" />
<input type="submit" class="button" id="submit-button" disabled="disabled" />
最佳答案
当您将按钮变成 jQuery UI 按钮时,您最好使用 Button 插件的 jQuery UI 方法来启用/禁用您的按钮,而不是直接更改 DOMElement 属性 disabled
.
对于初始状态,您初始化 Button,然后仅更改
disabled
属性,因此 Button 插件会考虑更改。// either disable first the button then initialize $( 'input[type="submit"]' ).attr( 'disabled', true ); $( '.button' ).button ();
或
// set the Button's option $( '.button' ).button (); $( 'input[type="submit"]' ).button('option', 'disabled', true);
对于第二个问题,使用按钮的选项 setter 重新启用您的按钮:
$( 'input[type="submit"]' ).button('option', 'disabled', false);
关于javascript - jquery:禁用/启用按钮在重置后不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10707229/