如果输入没有数组,如何禁用按钮?

时间:2013-02-18 15:32:37

标签: javascript jquery html css

我在HTML中有以下内容,输入值将由用户选择填充,并以数组格式(45[45, 6, 33], 67[67,2,5] and so on..)存储值。基本上,价值如下:

<input id="question_string" type="hidden" value="{}">
<input class="submit_updates" name="commit" type="submit" value="Process">

现在我需要禁用提交按钮,或者如果输入在{}中没有数组,则提醒一些消息,例如“选择所有值”。

更新:

var question_hash_element = document.getElementById('question_string');
var question_hash = JSON.parse(question_hash_element.value);

   var myArray = new Array();
      myArray[0] = window.batch_question_id;
      myArray[1] = window.answer_id || window.answer_text || window.batch_answer_checkbox
      myArray[2] = window.build_id

以上代码将这些值存储到{}中。我只想禁用并让用户选择所有字段来处理表单。如果值为{},则应禁用该按钮。和内部的任何值都应该启用。

我尝试过如下:

 $('.submit_updates').click(function () {
    if ($('#question_string') == {}) {
      return false;
      alert("Select all the Values");
    } else {
      return true;
    }
  });

它不起作用..

任何帮助将不胜感激!感谢

3 个答案:

答案 0 :(得分:2)

$('.submit_updates').on('click', function (e) {
   e.preventDefault();
   if ( $.trim($('#question_string').val())=='{}' ) {
       alert('no question !');
   }else{
       this.form.submit();
   }
});

答案 1 :(得分:1)

在提醒消息之前,您将返回false。

试试这个:

$('.submit_updates').on("click", function () {
  if ($('#question_string').val() == "{}") { //Adjusted condition
    //Alert message
    alert("Select all the Values");

    //Disable the submit button
    $(".submit_updates").prop("disabled", true);
    return false;
  } else {
  return true; //Not really needed
 }
});

正如@adeneo建议的那样,使用onprop代替clickattr会更好。

答案 2 :(得分:1)

试试这个

$(function(){
     $('.submit_updates').on('click', function () {
        if ($('#question_string').val() == "{}") {
              $(this).prop('disabled', 'disabled'); 
              alert("Select all the Values");
        } else {
        }
      });
});

DEMO