Click here to Skip to main content
15,881,881 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
my code is here,when the submit button clicked ,the form cannot be post.and i add some code in js
C#
else if (oCheck[i].type == 'button') {
               oCheck[i].onclick = function () {
                   alert("ddddd");
               }
           }

to see what happened with the button ,but the expected messagebox "ddddd" was not even popup.what's wrong with my code?
HTML
@using (Html.BeginForm())
{
    @Html.ValidationSummary(true)
    <fieldset>
        
       <div id="somecheckbox">
       <input type="checkbox" value="somevalue" />
       <input type="checkbox" value="somevalue" />
       <input type="checkbox" value="somevalue" />
       </div>

	<input type="text" id="edittobeset" />
        <p>
            <input type="submit" value="submit" />
            <input type="reset"  value="reset" />
        </p>
    </fieldset>
}

<script type="text/javascript">

    window.onload = function () {
        var divCheck = document.getElementById("somecheckbox");
        var oCheck = divCheck.getElementsByTagName("input");
        var oSet = document.getElementById("edittobeset");
        
        for (var i = 0; i < oCheck.length; i++) {
            if (oCheck[i].type == 'checkbox') {
                oCheck[i].onclick = function () {

                    if (this.checked == true) {
                        oSet.value += this.value + ".";
                    }
                }
            }
            else if (oCheck[i].type == 'button') {
                oCheck[i]. önclick = function () {
                    alert("ddddd");
                }
            }
        }
   }
</script>
Posted
Updated 6-Jun-12 8:58am
v5

The problem you have is the control you're looking for is not in the collection your iterating.

The for loop:
JavaScript
for (var i = 0; i < oCheck.length; i++)

Is checking the oCheck object for controls. oCheck is:
JavaScript
var oCheck = divCheck.getElementsByTagName("input");

divCheck is:
JavaScript
var divCheck = document.getElementById("somecheckbox");

This means, in your for loop, you checking the contents of 'somecheckbox' for input elements. The input element you're trying to find isn't a child to 'somecheckbox' and as such would not part of the oCheck collection.

As you're only targeting a single submit button I'd change the code to the following:

JavaScript
window.onload = function () {
       var divCheck = document.getElementById("somecheckbox");
       var oCheck = divCheck.getElementsByTagName("input");
       var oSet = document.getElementById("edittobeset");

       for (var i = 0; i < oCheck.length; i++) {
           if (oCheck[i].type == 'checkbox') {
               oCheck[i].onclick = function () {

                   if (this.checked == true) {
                       oSet.value += this.value + ".";
                   }
               }
           }
       }

       var oSubmit = document.getElementsByTagName('submit')[0];
       oSubmit. önclick = function () {
                   alert("ddddd");
               }

  }
 
Share this answer
 
v3
Comments
lamparded 6-Jun-12 14:37pm    
even i add a <input type="button" /> in the form. there is no chanage .the ddddd messagebox not displayed .
taha bahraminezhad Jooneghani 6-Jun-12 16:48pm    
change button to submit!
Stephen Hewison 6-Jun-12 17:04pm    
Sorry, just found the problem. Going to rewrite the solution!
Wonde Tadesse 6-Jun-12 20:35pm    
You don't need to evaluate "if (this.checked == true){ ... }" Instead use "if(this.checked) { ...}"
i made a mistake. type a wrong letter in my action function name.
 
Share this answer
 
Comments
lamparded 6-Jun-12 20:41pm    
public ActionResult RestaurantAdd()
[HttpPost]
public ActionResult RestaurandAdd()

here ,the Restaurand <--> Restaurant

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900