如何使用jQuery从两个输入中获取值

时间:2019-04-12 08:44:50

标签: javascript php jquery ajax

我有两个输入字段:

0.2

我正在尝试:

 <input type="text" class="form-control input-td" name="getmark[]" id="getMark_<?php echo $i++?>"  required>
 <input type="text" class="form-control input-td" name="mcq[]" id="mcq_<?php echo $i++?>" required>

我不确定如何拨打 $('input[id^="getMark_"]','input[id^="mcq_"]').on('change', function() { var get_mark = this.value; var mcq = this.value; });

如何调用两个输入字段。

4 个答案:

答案 0 :(得分:2)

您遇到语法错误,输入内容适合您的情况

_fundclass:any;
fundclass1:any;

get fundclass(): any {
    return this._fundclass;
}

@Input()
set fundclass(fundclass: any) {
    if (this.fundclass !== undefined ) {
        this._fundclass =  Object.keys(this.fundclass).map(key => ({type: key, value: this.fundclass[key]}));
    }
}
$('input[id^="getMark_"], input[id^="mcq_"').on('input', function() {  
		var mark = mcq = '';
    if($(this).attr("id").indexOf('getMark_') !== -1){
      mark = this.value;
    }else{
    	mcq = this.value
    }
		console.log(mark,mcq);
});

它将在输入文本框中得到更改。

答案 1 :(得分:1)

适用于您的代码:

$(document).on('change','input[id^=getMark_], input[id^=mcq_]', function() {  
       var get_mark = this.value;
       var mcq = this.value;
 });

因此,您不需要在属性选择器内使用“”或“ ...” 但是您也有逻辑错误,您的get_mark和mcq变量将始终相同。

因此,您需要两种单独的方法来获取这些变量,这会更好地选择

$(document).on('change','input[id^=getMark_]', function() {  
           var get_mark = this.value;
     });

$(document).on('change','input[id^=mcq_]', function() {  
           var get_mark = this.value;
     });

如果需要,可以将它们保存到全局变量。

答案 2 :(得分:0)

您可以执行类似的操作

 $(' input[name="getmark[]"], input[name="mcq[]"] ').on('change', function() {  
       var get_mark = $('input[name="getmark[]"]').val();
       var mcq      = $('input[name="mcq[]"]').val();
       
       console.log('get_mark', get_mark);
       console.log('mcq', mcq);

 });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="text" class="form-control input-td" name="getmark[]" id="getMark_<?php echo $i++?>"  required>
 <input type="text" class="form-control input-td" name="mcq[]" id="mcq_<?php echo $i++?>" required>

答案 3 :(得分:0)

用替换HTML:

  <div id='student_<?php echo $student_id; //$i++; ?>' class='student'>
  <input type="text" class="form-control input-td" class="getmark" name="getmark[]"    id="getMark_<?php echo $i++?>"  required>
  <input type="text" class="form-control input-td"  class="mcq"  name="mcq[]" id="mcq_<?php  echo $i++?>" required>
  <input type="text" class="form-control input-td" class="is_success" readonly>
 </div>

通过以下方式替换您的js:

$('.getmark, .mcq').on('change', function() {  
   var get_mark = this.value;
   var mcq      = this.value;
  if(get_mark > 18)
    $(this).find('.is_success').val()='pass';
   else 
     $(this).find('.is_success').val()='fail';
});

要收集所有这些信息:

function getallstudentmark()
{
arr_allstudents = new Array();

  $(.student).each(function (index, value) {
    obj_student = new Object();
    obj_student.id= $(this).attr('id');
    obj_student.get_mark = $(this).find('.getmark').val();
    obj_student.mcq = $(this).find('.mcq').val();
    obj_student.success = $(this).find('.is_success').val();
    arr_allstudents.push(obj_student);
  });
return arr_allstudents;
}