Jquery 取值,賦值學習總結

來源:互聯網
上載者:User

標籤:

<h2>擷取和設定文字框值:</h2> 
<input type="button" value="賦值檔案框" id="verifyButton" onclick="Show_xxdxxd(‘vvvvvv‘)"/>
<input type="text" name="xxdxxd" value="" id="xxdxxd" />
  <P>擷取檔案框值:
  <input type="button" value="讀取檔案框值" id="verifyButton" onclick="get_xxdxxd()"/>
  <div id="get_info"></div>
<p>
<script>
function Show_xxdxxd(info)
{
   //賦值操作
   $("#xxdxxd").val(info);
}

function get_xxdxxd()
{
   //取值操作
   val = $("#xxdxxd")[0].value;
   //alert(val);
   //val = $("#id")[0].value;
   $("#get_info").html(val);
}
</script> 

  
<h2>擷取和設定單選項radio的值:</h2>
<input type="radio" name="_radio" value="1">
<input name="_radio" type="radio" value="2" checked="checked">
<input type="button" value="賦值檔案框" onclick="Show_redio()"/>


<input type="radio" name="radio_type" value="3">
<input name="radio_type" type="radio" value="4" checked="checked">
<input type="button" value="賦值檔案框" onclick="Show_redio()"/>
  
<script>
function Show_redio()
{
     //擷取單選項值 $("input[@type=radio][@checked]").val();
  //第一種方法, 取得選中狀態的值
  _val = $("input[@type=radio][@checked]").val();
  //第二種方法,直接用radio的name進行操作, 取得選中狀態的值  擷取一組radio被選中項的值
  __val = $("input[@name=radio_type][@checked]").val();
 
  //radio單選組的第一個元素為當前選中值
  $("input[@type=radio]").get(0).checked = true;
  $("input[@name=radio_type]").get(0).checked = true;
  //alert(_val);
  //alert(__val); 
}
</script>


<h2>擷取和設定複選框的值:</h2>
<input type="checkbox" name="check" value="A" id="checkbox_id1">A  
<input type="checkbox" name="check" value="B" id="checkbox_id2">B
<input name="check" type="checkbox" value="C" checked="checked" id="checkbox_id3">
C  
<input type="button" value="賦值檔案框" onclick="Show_checkbox()"/>
<script>
function Show_checkbox()
{
     //取得多選框值
  //多選框checkbox:$("#checkbox_id").attr("value");
  val3 = $("#checkbox_id3").attr("value");
  val2 = $("#checkbox_id2").attr("value");
  val1 = $("#checkbox_id1").attr("value");
 
  //多選框checkbox:  $("#chk1").attr("checked",‘‘);//不打勾
     //$("#chk2").attr("checked",true);//打勾
     //if($("#chk1").attr(‘checked‘)==undefined) //判斷是否已經打勾
 
  //多選框checkbox 不打勾
  $("#checkbox_id3").attr("checked",‘‘);
  //多選擇框checkbox打勾
  $("#checkbox_id1").attr("checked", true);
  $("#checkbox_id2").attr("checked", true);
 
  if($("#checkbox_id1").attr(‘checked‘)==undefined)
    alert("沒有選中!");
  else
    alert("已經選中!");
}
</script>

<h2>擷取和設定下拉式功能表列表的值:</h2>
<select name="_select" id="_select" onchange="Show_select()">
<option value="A">A</option>
<option value="B">B</option>
<option value="C">C</option>
<option value="D">D</option>
</select>
<script>
function Show_select()
{
     //擷取select被選中項的文本
  var selected = $("select[@name=_select] option[@selected]").text();
  //alert(selected);
 
  //設定select下拉框的第二個元素為當前選中值
  $("#_select")[0].selectedIndex = 2;
 
  //下拉框select
  //設定一下值為D的項目為當前選中項
  $("#_select").attr("value",‘D‘);
 
  //添加下拉框的option
  $("<option value=‘E‘>E</option><option vlaue=‘F‘>F</option>").appendTo("#_select");
 
  //清空下拉框
  $("#_select").empty();
}
</script>


<h2>擷取和設定文字框/文本域的值:</h2>
<textarea cols="55" rows="5" name="text" id="text"></textarea>
<input type="button" value="賦值檔案框" onclick="Show_textarea()"/>
<script>
function Show_textarea()
{
      //填充內容
   $("#text").attr("value", ‘asfsafasfsafasffadsfaasdfa‘);
  
   //清空內容
   //$("#text").attr("value","");
}
</script>

Jquery 取值,賦值學習總結

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.