在表单中设置默认参数

时间:2016-01-15 11:00:20

标签: javascript c# html

@{  List<System.Web.Mvc.SelectListItem> Month = new List<System.Web.Mvc.SelectListItem>();

}
<script type="text/javascript">


    function setImage(param) {
        document.getElementById('ReportParameter').value = param;
//        alert(document.getElementById('ReportParameter').value);
    }

</script>

<form id="reportParametersForm" method="GET" action="@string.Format("{0}/{1}", @Url.Content("~/Reports/View"), ViewBag.Report)">
<fieldset style="padding: 0.2em 0 1.2em 0; height: 50px">
    <legend style="margin: 0 1px 0 10px; padding: 3px 36px 3px 20px; background-color: #494949;
        color: white; font-size: 11pt;">
        @Html.Resource(String.Format("Report_{0}", ViewBag.Report as string))</legend>
    <table border="0" cellspacing="0" cellpadding="0" style="font-size: x-small">
       <input type="hidden" name="ReportParameter" id="ReportParameter" value=""/>
    </table>
    <div align="center">   
        <button class="button" id="Day" style="width:90px; height:46px" type="submit" onclick="setImage('1') ;">
            Day</button>

         <button class="button" id="Week" style="width:90px; height:46px" type="submit" onclick="setImage('2') ;">
            Week</button>

         <button class="button" id="Month" style="width:90px; height:46px" type="submit" onclick="setImage('3') ;">
            Month</button>           

    </div>
    <input type="hidden" name="ShowToolBar" value="false" />

</fieldset>
</form>
<script type="text/javascript">
    var links = $('button');

    links.click(function () {
        links.css('background-color', '#2B2B2B');
        $(this).css('background-color', '#BE4856');
    });
</script>

如何将图像1设置为默认选项,当我打开我有3个不同按钮的页面时,当我选择某个按钮时,我会看到不同的图像? 我希望在我的屏幕上看到图像1并且按钮1被选为默认值,但是当我选择按钮1,2,3以便能够看到其他图像时

2 个答案:

答案 0 :(得分:1)

创建函数setDefaults(),并在start时调用它 要制作更清晰的代码,您可以将html中相关图像的数据属性传递给每个按钮。

         <button class="button" id="Day" style="width:90px; height:46px" type="submit" data-image="1">
            Day</button>

         <button class="button" id="Week" style="width:90px; height:46px" type="submit" data-image="2">
            Week</button>

         <button class="button" id="Month" style="width:90px; height:46px" type="submit" data-image="3">
            Month</button>  

帮助函数setActiveButton()改变样式。

function setDefaults(buttonId) {
  var button = $('#' + buttonId);
  setActiveButton(buttonId);
  setImage(button.data('image'));
}

function setActiveButton(buttonId) {
  var links = $('button');
  links.css('background-color', '#2B2B2B');
  $('#' + buttonId).css('background-color', '#BE4856');
}

在您的脚本标记

<script type="text/javascript">
    var links = $('button');

    links.click(function () {
        setActiveButton(this.id);

    });
    setDefaults('Day');
</script>

答案 1 :(得分:0)

您可以在脚本加载默认值时运行该函数。

    function setImage (param) {
        document.getElementById('ReportParameter').value = param;
    }

    setImage(1);