【问题标题】:Validation jQuery not validating data correctly验证 jQuery 没有正确验证数据
【发布时间】:2014-11-04 02:30:13
【问题描述】:

我只希望用户能够在文本框中的值已填写时提交表单,而 JSON 数组我只希望用户能够在文本中的值时提交表单方框已被填充,图像的 JSON 数组已被拖入正确的顺序。

代码需要 image_order 以及文本框输入。这是因为它也被标记为名称吗?我怎样才能避免这种情况,但在提交之前仍然要求两个字段和顺序都是正确的?

对不起,我对这一切都很陌生。

JavaScript

$(window).load(function(){
$(function() {

$( "#sortable" ).sortable({
  placeholder: "ui-state-highlight",
  cursor: 'crosshair',
  update: function(event, ui) {
    var order = $("#sortable").sortable("toArray");
    order = JSON.stringify(order);
    correct = JSON.stringify(["4","3","2","1"]);
    if (order==correct){
      var valid = true
        console.log(':)');
    }
  }
});

$( "#sortable" ).disableSelection();

$('#submit').on('click', function() {
var valid = true,
    message = '';

$('form input').each(function() {
    var $this = $(this);

    if(!$this.val()) {
        var inputName = $this.attr('name');
        valid = false;
        message += 'Please enter your ' + inputName + '\n';
    }
});

if(!valid) {
    alert(message);
}
else {
  window.location.href = "http://stackoverflow.com";
}
});
});
});

HTML

Name : <input type="text" class="text" name="name" id="name" /> <br/>
Address : <input type="text" class="text" name="address" id="address" /> <br/>
email : <input type="text" class="text" name="email" id="email" /> <br/>

<input type="hidden" id="image_order" name="image_order" value="order" />

<ul id="sortable" style="width: 524px;">
<li id="1" class="ui-state-default"><img src="1.png" width="100" height="90" /></li>
<li id="2" class="ui-state-default"><img src="2.png" width="100" height="90" /></li>
<li id="3" class="ui-state-default"><img src="3.png" width="100" height="90" /></li>
<li id="4" class="ui-state-default"><img src="4.png" width="100" height="90" /></li>  
</ul>

<div style="clear:both;"></div>
<input name="Submit" value="RE-ORDER" type="submit" />
<input type="button" id="submit" value="Submit" name="submit" />
</form>
</body>

【问题讨论】:

    标签: javascript jquery html jquery-ui validation


    【解决方案1】:

    试试这个:

    $('form input').not('#image_order').each( //...
    

    【讨论】:

    • 这似乎允许代码验证表单而不是画廊的顺序:(
    猜你喜欢
    • 1970-01-01
    • 2013-12-18
    • 2013-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-19
    • 2023-02-02
    相关资源
    最近更新 更多