2017-04-13 36 views
0

我有很多pf的投入,我想在(「變」)獲得的價值或輸入一個字符串,這樣我可以在以後轉換爲名稱一個班級,並注入到一個分區...我可能不是最清晰的,但...任何幫助是好的!JS入門存儲的輸入值/名稱的VAR

網站:http://angusweb.pw

編輯:使用的CSS類是從上面的網站上的框架,我正在開發它

它的下面綠色的大廣場和HTML是

HTML

<div class="setFullWH setFlex setFlexSuperCenter setFlexWrap "> 

<form id="parentProperties" class="setFlex setFlexWrap setFlexRow"> 
<div class="setFlex setBasicGrow setBasicShrink setFlexBasis15 setFlexColumn setNoto"> 
<div class="setBasicGrow setJustifyContentSpcArnd"> 
<input id="flexParent1" name="flexParent" type="radio"> 
<label for="flexParent1">seFlexRow</label> 
</div> 
<div class="setBasicGrow"> 
<input id="flexParent2" name="flexParent" type="radio"> 
<label for="flexParent2">setFlexColumn</label> 
</div> 
<div class="setBasicGrow"> 
<input id="flexParent3" name="flexParent" type="radio"> 
<label for="flexParent3">setFlexHAlign</label> 
</div> 
</div> 
<div class="setFlex setBasicGrow setBasicShrink setFlexBasis15 setFlexColumn setNoto"> 
<div class="setBasicGrow"> 
<input id="flexParent4" name="flexParent" type="radio"> 
<label for="flexParent4">setFlexVAlign</label> 
</div> 
<div class="setBasicGrow"> 
<input id="flexParent5" name="flexParent" type="radio"> 
<label for="flexParent5">setFlexSuperCenter</label> 
</div> 
<div class="setBasicGrow"> 
<input id="flexParent6" name="flexParent" type="radio"> 
<label for="flexParent6">setAlignContentStart</label> 
</div> 
</div> 
<div class="setFlex setBasicGrow setBasicShrink setFlexBasis15 setFlexColumn setNoto"> 
<div class="setBasicGrow"> 
<input id="flexParent7" name="flexParent" type="radio"> 
<label for="flexParent7">setAlignContentEnd</label> 
</div> 
<div class="setBasicGrow"> 
<input id="flexParent8" name="flexParent" type="radio"> 
<label for="flexParent8">setAlignContentCenter</label> 
</div> 
<div class="setBasicGrow"> 
<input id="flexParent9" name="flexParent" type="radio"> 
<label for="flexParent9">setAlignContentStretch</label> 
</div> 
</div> 
<div class="setFlex setBasicGrow setBasicShrink setFlexBasis15 setFlexColumn setNoto"> 
<div class="setBasicGrow"> 
<input id="flexParent10" name="flexParent" type="radio"> 
<label for="flexParent10">setAlignContentSpcBetw</label> 
</div> 
<div class="setBasicGrow"> 
<input id="flexParent11" name="flexParent" type="radio"> 
<label for="flexParent11">setAlignContentSpcArr</label> 
</div> 
<div class="setBasicGrow"> 
<input id="flexParent12" name="flexParent" type="radio"> 
<label for="flexParent12">setAlignItemsStart</label> 
</div> 
</div> 

<div class="setFlex setBasicGrow setBasicShrink setFlexBasis15 setFlexColumn setNoto"> 
<div class="setBasicGrow"> 
<input id="flexParent13" name="flexParent" type="radio"> 
<label for="flexParent13">setAlignItemsEnd</label> 
</div> 
<div class="setBasicGrow"> 
<input id="flexParent14" name="flexParent" type="radio"> 
<label for="flexParent14">setAlignItemsCenter</label> 
</div> 
<div class="setBasicGrow"> 
<input id="flexParent15" name="flexParent" type="radio"> 
<label for="flexParent15">setAlignItemsStretch</label> 
</div> 
</div> 

    <div class="setFlex setBasicGrow setBasicShrink setFlexBasis15 setFlexColumn setNoto"> 
    <div class="setBasicGrow"> 
    <input id="flexParent16" name="flexParent" type="radio"> 
    <label for="flexParent16">setAlignItemsBaseline</label> 
    </div> 
    <div class="setBasicGrow"> 
    <input id="flexParent17" name="flexParent" type="radio"> 
    <label for="flexParent17">setFlexWrap</label> 
    </div> 
    </div> 
    </form> 
</div> 

編輯:我設法得到我想要的值,並將其轉換爲類,並注入想要的div ...問題是,當ic舔其他一些輸入,我想前者生成的類將被刪除或替換,由新一...

+0

所以你想獲得投入的價值?嘗試並指定核心問題,它可以幫助我們和你:) – dodo

+0

此鏈接將幫助你http://stackoverflow.com/questions/20754131/take-input-value-and-display-in-another-div [] [ 1] –

+0

[這應該有助於解決您的問題](http://stackoverflow.com/questions/20754131/take-input-value-and-display-in-another-div) –

回答

0

你可以得到一個輸入字段的名稱和值在JavaScript中的以下內容:

var inputs = document.getElementsByTagName('input'); 
 
var value = inputs[0].value; 
 
var name = inputs[0].name; 
 

 
console.log(value); 
 
console.log(name);
<input id="inputText" type="text" name="fname" value="a value">

+0

如果我喜歡40個輸入?這對所有人來說都不是很實際... – Miguel

+0

@Miguel我編輯了答案來選擇一個輸入列表。您可以執行此操作或通過特定名稱或ID選擇輸入。 – Tony

1

你可以使用jQuery?如果是的話

$('input:radio').each(function(){ 
    $(this).on("click",function(){ 
     console.log($(this).val()); //To get the value, this returns on/off wether your radio is selected or not 
     console.log($(this).attr('name')); // To get the name 
     console.log($(this).next('label').text()); //To get the text inside your label 
    }); 
}) 
+0

我可以把它變成一個可重用的功能嗎? – Miguel

+0

如何?如果你只是把它放在'$(document).ready(function(){})'中,腳本就會運行,你的onclick事件將被設置爲你的輸入。 – Lixus

0

您可以在HTML中添加此新元素(<div class="class-name"></div>),並在其類中注入所選輸入(無線電)的名稱。這裏是代碼:

HTML

<div class="class-name"></div>

的JavaScript

$(document).ready(function(){ 
 
    $('#parentProperties input').on('click', function(){ 
 
    className = $(this).attr('name'); 
 
    $('.class-name').addClass(className); 
 
    }); 
 
});

而不是on.('change')on.('click')在這種情況下是完美的。