2

基本上,如果用户想要将它们发送到收件人电子邮件,我会尝试根据用户选择的数量显示 1 到 10 个文本框。

我有一个下拉选择,其中包含 1-10 个选项/值、两个单选按钮和十个支持 div 的文本框。我正在使用以前的开发人员(我所占据的位置)使用的代码片段,我只能假设它是 jQuery,但我有一些我所拥有的小 Javascript 知识与它混合在一起。不确定我是否可以这样做。:/ Javascript/jQuery 是我的克星。

这是我到目前为止所拥有的(实时服务器): http: //www.dealraiser.net/gift

使用的 Javascript/jQuery:

<script type="text/javascript">
// SHOW HIDDEN TEXTBOXES
function updateDisplay(select) {
    // ACCESS RADIO OBJECT
    var radioValue = getRadioCheckedValue('sending');
    // SHOW FIELD IF RADIO IS SET TO 'R'
    if (radioValue == 'r') { showField(); }
}

// BORROWED FROM GOOGLE - EASIER TO USE PRE-EXISTING
function getRadioCheckedValue(radio_name) {
   var oRadio = document.forms[0].elements[radio_name];

   for(var i = 0; i < oRadio.length; i++) {
      if(oRadio[i].checked) {
         return oRadio[i].value;
      }
   }

   return '';
}
// DISPLAY FIELD, HIDE OTHERS
function showField() {
    // HIDE ALL EXTRA OPTIONS
    for (i=1, i<10, i++) {
        $("#re"+i).hide();;
        $('#r'+i).removeClass('required');
    }

    if (radio.value == 'r') {
        // DETERMINE NUMBER OF EMAILS TO SHOW
        var giftNumber = document.getElementById('giftnumber').value;

        // DISPLAY ONLY THE RIGHT AMOUNT OF EMAIL INPUTS
        for (i=1, i<=giftNumber, i++) {
            $("#re"+i).show();
            $('#r'+i).addClass('required');
        }
    }
}

// TOGGLE RADIO ON/OFF
function doToggle(radio) {
    // HIDE ALL EXTRA OPTIONS
    for (i=1, i<10,i++) {
        $("#re"+i).hide();
        $('#r'+i).removeClass('required');
    }

    if (radio.value == 'r') {
        // DETERMINE NUMBER OF EMAILS TO SHOW
        var giftNumber = document.getElementById('giftnumber').value;

        // DISPLAY ONLY THE RIGHT AMOUNT OF EMAIL INPUTS
        for (i=1, i<=giftNumber, i++) {
            $("#re"+i).show();
            $('#r'+i).addClass('required');
        }
    }
}

</script>

这是HTML:

<h3>1. How many subscriptions would you like to gift?   
    <select name="giftnumber" size="1" id="giftnumber" onChange="updateDisplay(this)" style="width: 48px">
      <option value="1">1</option>
      <option value="2">2</option>
      <option value="3">3</option>
      <option value="4">4</option>
      <option value="5">5</option>
      <option value="6">6</option>
      <option value="7">7</option>
      <option value="8">8</option>
      <option value="9">9</option>
      <option value="10">10</option>
  </select></h3>
  <h3>2. Sending options</h3>
  <p>
    <label>
      <input type="radio" name="sending" id="sending" value="b" onChange="doToggle(this)" />
      Send the pre-paid codes to my email and I will forward them.</label>
  </p>
  <p>
    <label>
      <input type="radio" name="sending" id="sending" value="r" onChange="doToggle(this)" />
      Send the pre-paid codes to the recipient email address(es).</label>
  </p>
  <div id="re1" name="re1" style="display:none;">
      <p>Recipient Email: <input name="r1" id="r1" type="text" /></p>
  </div>
  <div id="re2" name="re2" style="display:none;">
      <p>Recipient Email: <input name="r2" id="r2" type="text" /></p>
  </div>
  <div id="re3" name="re3" style="display:none;">
      <p>Recipient Email: <input name="r3" id="r3" type="text" /></p>
  </div>
  <div id="re4" name="re4" style="display:none;">
      <p>Recipient Email: <input name="r4" id="r4" type="text" /></p>
  </div>
  <div id="re5" name="re5" style="display:none;">
      <p>Recipient Email: <input name="r5" id="r5" type="text" /></p>
  </div>
  <div id="re6" name="re6" style="display:none;">
      <p>Recipient Email: <input name="r6" id="r6" type="text" /></p>
  </div>
  <div id="re7" name="re7" style="display:none;">
      <p>Recipient Email: <input name="r7" id="r7" type="text" /></p>
  </div>
  <div id="re8" name="re8" style="display:none;">
      <p>Recipient Email: <input name="r8" id="r8" type="text" /></p>
  </div>
  <div id="re9" name="re9" style="display:none;">
      <p>Recipient Email: <input name="r9" id="r9" type="text" /></p>
  </div>
  <div id="re10" name="re10" style="display:none;">
      <p>Recipient Email: <input name="r1" id="r10" type="text" /></p>
  </div>
4

2 回答 2

2

There is ';' missing at line 180

before:

for (i=1, i<10, i++)

after:

for (i=1; i<10; i++)

于 2012-08-23T23:18:08.433 回答
0

Use something like this:

function showtextareas(sel) {
    var value = sel.options[sel.selectedIndex].value;  
    if(value==1) { 
       //show one textarea 
    }
    else if(...) { ... }
}

<select onchange="showtextareas(this)">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
// ......
<option value="10">10</option>
</select>

BTW I dunno why color formatting is weird...

于 2012-08-23T23:18:23.067 回答