2016-04-27 16 views
0

Ich habe den folgenden CodeWie wird man Wert von Textfeld in Zelle embeded

<tr val='question'> 
    <td> 
     <input style='width: 500px' type='text' placeholder='Q.Enter your question here for radio button? '> 
</tr> 

Wie kann ich den Wert des Eingabefeldes in Zelle eingebettet finden.

function saveUserDefQues(){ 

var table=document.getElementById("QuestionList"); 
var surveyquestionform=document.forms[0]; 
var count=$('#QuestionList tr').length 
for (var i = 0; i<count; i++) { 
var row = table.rows[i]; 
if(row.getAttribute('val')==='question') 
    { 
    var Cells = row.getElementsByTagName("td");; 
    } 

}  

}

+0

Unter welchem ​​Ereignisse auf welchem ​​Elemente versuchen Sie, diese 'input' zu finden? Beachten Sie außerdem, dass Ihr HTML-Code ungültig ist. 'val' ist kein gültiges Attribut für das' tr', und Sie vermissen ein '' –

+0

Können Sie dem Eingabeelement eine 'id' hinzufügen? – MarcoS

+0

gibt es eine Schaltfläche und onclick sogar ich bin alle Wert dieses Eingabefeldes abrufen – abhishek

Antwort

1
document.querySelector('tr[val] > td > input').value; 

Array.from(document.querySelectorAll('tr[val] > td > input')).forEach(function(entry, index, entries) 
{ 
    entry.value; // you may store the value OR process with it AS you see fit 
}); 
1

Da Sie Jquery verwenden diese auf diese Weise durchgeführt werden kann.

ersetzen diese Codezeile

var Cells = row.getElementsByTagName("td");

mit

var Cells = $(row).find('td');

var inputValue = Cell.find('input').val(); // gives you value of input


-Code Refactoring recomme NDED

Ich möchte Ihr Code Refactoring, wie unten

HTML

<tr data-val='question'>  // use data-* attribute to add custom attributes into tags 
    <td> 
     <input style='width: 500px' type='text' placeholder='Q.Enter your question here for radio button? '> 
    </td>     // close your td 
</tr> 

Script

function saveUserDefQues(){  

var surveyquestionform = document.forms[0]; // not sure what this is for, so ill leave it as is. 

$('#QuestionList tr[data-val="question"]').each(function(){ //loop all tr's which has the data-val set to question  
    var inputValue = $(this).find('td input').val();   //get the value of input 
    console.log(inputValue);    
}); 

} 
+0

danke, es hat funktioniert. – abhishek

0

$("tr[val='question'] > td > input").val()

Aber zuerst müssen Sie ein gültiges HTML schreiben. </td> schließendes Tag fehlt. Auch müssen Sie diese tr in eine <table> setzen.

0

See this Plunker

function getResult(){ 
    $("tr").each(function(index) { 
    console.log($(this).find('input').attr('placeholder')); 
}); 
} 
Verwandte Themen