2012-11-28 33 views
0

所以表真的應該像下面這樣當用戶訪問該頁面:如何禁用文本輸入中顯示的值

Marks Per Answer            Total Marks Marks Remaining 
(blank text input)             4    4 
(blank text input)             6    6 
(disabled text input = 4 (same value as under Total Marks))  4    0 

我的問題是,怎樣才能既以上步驟可以通過改變解決jQuery的下面:

$(function() { 
//alert("here");   
var questions = $('#markstbl td[class*="_ans"]').length; 

//disable single entry 
for (var i=0;i<=questions;i++){ 
if($("[class*=q"+i+"_mark]").length ==1){ 
var t_marks = $("[class*=q"+i+"_ans]").html(); 
//alert(t_marks); 
$("[class*=q"+i+"_mark]").val(t_marks).attr('readonly',true); 
//$("[class*=q"+i+"_mark]").attr('readonly',true); 
}      
} 

//find each question set and add listeners 
for (var i=0;i<=questions;i++){          
$('input[class*="q'+i+'"]').keyup(function(){ 
var cl = $(this).attr('class').split(" ")[1] 
var questionno = cl.substring(cl.indexOf('q')+1,cl.indexOf('_')); 
var tot_marks = $(".q"+questionno+"_ans_org").val(); 
//alert(tot_marks); 
var ans_t=0; 
$("[class*=q"+questionno+"_mark]").each(function(){ 
var num = (isNaN(parseInt($(this).val())))?0:parseInt($(this).val()); 
ans_t+=parseInt(num);        
}); 
ans_t=tot_marks-ans_t;        
//alert(ans_t); 
//var fixedno = tot_marks; 
var ans = ans_t; 
var answerText='<strong>'+ans+'</strong>'; 
$(".q"+questionno+"_ans").val(ans); 
$(".q"+questionno+"_ans_text").html(answerText); 
}); 
} 
}); 

下面是動態HTML表:

<form id="Marks" action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post"> 


<table border='1' id='markstbl'> 
<thead> 
<tr> 
<th class='questionth'>Question No.</th> 
<th class='questionth'>Question</th> 
<th class='answerth'>Answer</th> 
<th class='answermarksth'>Marks per Answer</th> 
<th class='totalmarksth'>Total Marks</th> 
<th class='noofmarksth'>Marks Remaining</th> 
</tr> 
</thead> 
<tbody> 
<?php 
$row_span = array_count_values($searchQuestionId); 
$prev_ques = ''; 
foreach($searchQuestionId as $key=>$questionId){ 

?> 

<tr class="questiontd"> 
    <?php 
    if($questionId != $prev_ques){ 
    ?> 
    <td class="questionnumtd" name="numQuestion" rowspan="<?php echo$row_span[$questionId]?>"><?php echo$questionId?> <input type="hidden" name="q<?php echo$questionId?>_ans_org" class="q<?php echo$questionId?>_ans_org" value="<?php echo$searchMarks[$key]?>"><input type="hidden" name="q<?php echo$questionId?>_ans" class="q<?php echo$questionId?>_ans" value="<?php echo$searchMarks[$key]?>"></td> 
    <td class="questioncontenttd" rowspan="<?php echo$row_span[$questionId]?>"><?php echo$searchQuestionContent[$key]?> </td> 
    <?php 
    } 
    ?> 
<td class="answertd" name="answers[]"><?php echo$searchAnswer[$key]?></td> 
<td class="answermarkstd"> 
<input class="individualMarks q<?php echo$questionId?>_mark_0" q_group="1" name="answerMarks[]" id="individualtext" type="text" /> 
</td> 
<?php 
    if($questionId != $prev_ques){ 
    ?> 
<td class="totalmarkstd" rowspan="<?php echo$row_span[$questionId]?>"><?php echo$totalMarks[$key]?></td> 
<td class="noofmarkstd q<?php echo$questionId?>_ans_text" q_group="1" rowspan="<?php echo$row_span[$questionId]?>"><?php echo"<strong>".$searchMarks[$key]."</strong>"?></td> 
<?php 
    } 
    ?> 
</tr> 
<?php 
$prev_ques = $questionId; 
} 
?> 
</tbody> 
</table> 
</form> 

在媽媽耳鼻喉科在jQuery的功能,我試圖用t_marks變量顯示在文本輸入的值,但是當我提醒t_marks

回答

1

可以使用的只讀,而不是禁用

的變化是什麼都不顯示

$("[class*=q"+i+"_mark]").val(t_marks).attr("disabled","disabled"); 

$("[class*=q"+i+"_mark]").val(t_marks).attr('readonly',true); 
+0

我試圖將其更改爲只讀,但它仍然不顯示只讀文本框中的值。你能看到我在jquery怎麼搞錯了,就像我提到的那樣,當我嘗試使用'_marks時,它沒有在aleart中顯示任何數字,它對我說它無法檢索到值 – user1830984

+0

我做了一個例子來檢查如果我們可以將文本分配爲只讀http://jsfiddle.net/mJxMb/ – Adil

+0

它必須是錯誤的t_marks變量,因爲我更新了你的小提琴,它仍然使更改[小提琴](http://jsfiddle.net/mJxMb /)。你可以看看是否有任何jQuery代碼錯誤?我已經包含完整的jQuery代碼以及完整的php/html代碼 – user1830984

0

或者你可以讓它專注了,當它獲得焦點

<input type="text" id="myTxt" onfocus="$(this).blur()"/> 
+0

爲了做到這一點,jquery需要編輯什麼? – user1830984

相關問題